<template>
  <div class="friends-page">
    <el-card class="content-card">
      <div class="page-header">
        <h1>友情链接</h1>
        <div class="header-divider">
          <span class="line"></span>
          <i class="fas fa-link"></i>
          <span class="line"></span>
        </div>
        <p>与优秀的人同行，分享技术与生活</p>
        <div class="site-info">
          <div class="site-avatar">
            <el-avatar class="avatar" :src="$store.state.webSiteInfo.logo" />
            <div class="avatar-border"></div>
          </div>
          <div class="site-details">
            <h2>{{ $store.state.webSiteInfo.name }}</h2>
            <p>{{ $store.state.webSiteInfo.summary }}</p>
            <div class="site-url">
              <i class="fas fa-link"></i>
              <input type="text" :value="$store.state.webSiteInfo.webUrl" readonly ref="urlInput" @click="copyUrl">
              <button class="copy-btn" @click="copyUrl">
                <i class="fas fa-copy"></i>
              </button>
            </div>
          </div>
        </div>
        <div class="copy-tip">
          <i class="fas fa-info-circle"></i>
          申请友链前请先添加本站链接
          <span class="tip-highlight">「 点击上方链接可快速复制 」</span>
        </div>
        <button class="apply-btn" @click="handleApply">
          <i class="fas fa-plus"></i>
          申请友链
        </button>
      </div>

      <div class="friends-container">
        <div class="section-title">
          <h2>友链列表</h2>
          <span class="count">{{ friends.length }} 个伙伴</span>
        </div>
        <div class="friends-grid">
          <div v-for="friend in friends" :key="friend.id" class="friend-card" @click="visitFriend(friend.url)">
            <div class="friend-avatar">
              <img :src="friend.avatar" :alt="friend.name">
              <div class="status" :class="{ online: friend.online }"></div>
            </div>
            <div class="friend-info">
              <h3>{{ friend.name }}</h3>
              <p>{{ friend.info }}</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 申请表单弹窗 -->
      <el-dialog :visible.sync="showApplyForm" title="申请友链" width="500px" top="3vh">
        <div class="apply-form">
          <el-form size="small" :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <div class="form-group">
              <el-form-item prop="name">
                <template v-slot:label>
                  <i class="fas fa-signature"></i> 网站名称
                </template>
              </el-form-item>
              <el-input type="text" v-model="form.name" placeholder="请输入您的网站名称" />
            </div>
            <div class="form-group">
              <el-form-item prop="url">
                <template v-slot:label>
                  <i class="fas fa-link"></i> 网站链接
                </template>
              </el-form-item>
              <el-input type="url" v-model="form.url" placeholder="请输入您的网站链接" />
            </div>
            <div class="form-group">
              <el-form-item prop="info">
                <template v-slot:label>
                  <i class="fas fa-quote-left"></i> 网站描述
                </template>
              </el-form-item>
              <el-input type="url" v-model="form.info" placeholder="一句话描述您的网站" />
            </div>
            <div class="form-group">
              <el-form-item prop="avatar">
                <template v-slot:label>
                  <i class="fas fa-image"></i> 头像链接
                </template>
              </el-form-item>
              <el-input type="url" v-model="form.avatar" placeholder="请输入您的头像链接" />
            </div>
            <div class="form-group">
              <el-form-item prop="email">
                <template v-slot:label>
                  <i class="fas fa-envelope"></i> 联系邮箱
                </template>
              </el-form-item>
              <el-input type="url" v-model="form.email" placeholder="邮箱用于联系" />
            </div>

            <el-form-item class="form-footer">
              <el-button class="submit-btn" @click="submitApplication">
                <i class="fas fa-paper-plane">
                </i>提交申请
              </el-button>
            </el-form-item>
          </el-form>

        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getFriendsApi, applyFriendApi } from '@/api/friends'

export default {
  name: 'Friends',

  data() {
    return {
      showApplyForm: false,
      form: {
        name: '',
        url: '',
        info: '',
        avatar: '',
        email: ''
      },
      friends: [],
      rules: {
        name: [
          { required: true, message: '请输入网站名称', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入网站链接', trigger: 'blur' },
          { type: 'url', message: '请输入正确的链接格式', trigger: 'blur' }
        ],
        info: [
          { required: true, message: '请输入网站描述', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '请输入头像链接', trigger: 'blur' },
          { type: 'url', message: '请输入正确的链接格式', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入联系邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await getFriendsApi()
    this.friends = res.data
  },
  methods: {
    visitFriend(url) {
      window.open(url, '_blank')
    },
    handleApply() {
      this.form = {
        name: '',
        url: '',
        info: '',
        avatar: '',
        email: ''
      },
        this.showApplyForm = true
    },
    handleClose() {
      this.showApplyForm = false
    },
    submitApplication() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          try {
            const res = applyFriendApi(this.form)
            this.showApplyForm = false
            this.$message.success('申请已提交，请等待审核')
            this.$refs['ruleForm'].resetFields();
          } catch (error) {
            this.$message.error(error.message)
          }
        } else {
          console.log('error submit!!')
        }
      })

    },
    copyUrl() {
      const input = this.$refs.urlInput
      input.select()
      document.execCommand('copy')
      this.$message.success('链接已复制到剪贴板')
    }
  }
}
</script>

<style lang="scss" scoped>

:deep(.el-form-item__content){
  margin-left: 0 !important;
}
.friends-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: $spacing-xl;
}

.content-card {
  padding: $spacing-xl;
}

.page-header {
  text-align: center;
  margin-bottom: $spacing-xl * 2;

  h1 {
    font-size: 2.5em;
    color: var(--text-primary);
    margin-bottom: $spacing-lg;
    font-weight: 700;
    background: linear-gradient(135deg, $primary, $secondary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .header-divider {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: $spacing-md;
    margin-bottom: $spacing-lg;

    .line {
      width: 60px;
      height: 1px;
      background: linear-gradient(90deg,
          transparent,
          var(--text-secondary),
          transparent);
    }

    i {
      color: $primary;
      font-size: 1.2em;
      transform: rotate(45deg);
    }
  }

  p {
    color: var(--text-secondary);
    font-size: 1.2em;
    margin-bottom: $spacing-xl;
  }

  .apply-btn {
    padding: $spacing-md $spacing-xl;
    background: $primary;
    color: white;
    border: none;
    border-radius: 25px;
    font-size: 1em;
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: $spacing-sm;

    &:hover {
      transform: translateY(-2px);
      box-shadow: $shadow-lg;
      background: darken($primary, 5%);
    }
  }
}

.friends-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: $spacing-lg;
}

.friend-card {
  @include card;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  padding: $spacing-lg;
  display: flex;
  align-items: center;
  gap: $spacing-lg;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    border-color: $primary;
  }

  .friend-avatar {
    position: relative;
    width: 64px;
    height: 64px;
    flex-shrink: 0;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      object-fit: cover;
    }

    .status {
      position: absolute;
      bottom: 0;
      right: 0;
      width: 12px;
      height: 12px;
      border-radius: 50%;
      background: #9ca3af;
      border: 2px solid var(--card-bg);

      &.online {
        background: #10b981;
      }
    }
  }

  .friend-info {
    flex: 1;
    min-width: 0;

    h3 {
      color: var(--text-primary);
      font-size: 1.1em;
      margin-bottom: $spacing-xs;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    p {
      color: var(--text-secondary);
      font-size: 0.9em;
      line-height: 1.4;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }
  }
}

.site-info {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: $border-radius-lg;
  padding: $spacing-md $spacing-md * 1.5;
  margin: $spacing-md auto;
  max-width: 600px;
  display: flex;
  align-items: center;
  gap: $spacing-xl;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, $primary, $secondary);
  }

  .site-avatar {
    width: 100px;
    height: 100px;
    flex-shrink: 0;
    position: relative;

    .avatar {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      object-fit: cover;
      border: 2px solid var(--border-color);
      background: var(--card-bg);
    }
  }

  .site-details {
    flex: 1;
    min-width: 0;

    h2 {
      color: var(--text-primary);
      font-size: 1.5em;
      margin-bottom: $spacing-xs;
      font-weight: 600;
    }

    p {
      color: var(--text-secondary);
      margin-bottom: $spacing-sm;
      font-size: 1em;
      line-height: 1.5;
    }

    .site-url {
      display: flex;
      align-items: center;
      gap: $spacing-sm;
      padding: $spacing-sm;
      background: var(--input-bg);
      border-radius: $border-radius-md;
      border: 1px solid var(--border-color);

      i {
        font-size: 0.9em;
        color: var(--text-secondary);
      }

      input {
        background: none;
        border: none;
        color: var(--text-primary);
        font-size: 0.9em;
        flex: 1;
        min-width: 0;
        cursor: pointer;

        &:focus {
          outline: none;
        }
      }

      .copy-btn {
        background: none;
        border: none;
        color: var(--text-secondary);
        cursor: pointer;
        padding: $spacing-xs $spacing-sm;
        transition: all 0.3s ease;

        &:hover {
          color: $primary;
        }
      }
    }
  }
}

.copy-tip {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: $spacing-sm;
  color: var(--text-secondary);
  margin-bottom: $spacing-xl;
  font-size: 0.9em;

  i {
    color: $primary;
  }

  .tip-highlight {
    color: $primary;
    margin-left: $spacing-sm;
    font-size: 0.9em;
  }
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: $spacing-xl;
  padding-bottom: $spacing-md;
  border-bottom: 1px solid var(--border-color);

  h2 {
    color: var(--text-primary);
    font-size: 1.8em;
    font-weight: 600;
  }

  .count {
    color: var(--text-secondary);
    font-size: 1.1em;
  }
}

@include responsive(md) {
  .friends-page {
    padding: $spacing-lg;
  }

  .content-card {
    padding: $spacing-lg;
  }

  .site-info {
    flex-direction: column;
    text-align: center;
    padding: $spacing-lg;
    margin: $spacing-lg 0;

    .site-avatar {
      width: 80px;
      height: 80px;
    }

    .site-details {
      width: 100%;

      .site-url {
        margin: 0 auto;
        max-width: 300px;
      }
    }
  }
}

@include responsive(sm) {
  .friends-page {
    padding: $spacing-md;
  }

  .content-card {
    padding: $spacing-md;
    border-radius: 0;
    margin: -$spacing-md;
  }

  .friends-grid {
    grid-template-columns: 1fr;
  }

  .friend-card {
    border-radius: $border-radius-md;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}

@keyframes slideIn {
  from {
    transform: translateY(20px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes slideOut {
  from {
    transform: translateY(0);
    opacity: 1;
  }

  to {
    transform: translateY(20px);
    opacity: 0;
  }
}

@keyframes slideUp {
  from {
    transform: translateY(10px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.apply-form {

  .form-group {
    margin-bottom: $spacing-lg;
  }

  .form-footer {
    display: flex;
    justify-content: flex-end;
    gap: $spacing-md;

    button {
      padding: $spacing-sm $spacing-xl;
      border-radius: 20px;
      font-size: 0.95em;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      gap: $spacing-sm;

      i {
        font-size: 0.9em;
      }
    }

    .submit-btn {
      background: $primary;
      border: none;
      color: white;

      &:active {
        transform: translateY(0);
      }
    }
  }
}

@for $i from 1 through 5 {
  .form-group:nth-child(#{$i}) {
    animation: slideUp 0.4s ease-out forwards;
    animation-delay: #{$i * 0.1}s;
  }
}
</style>